package demo;

import java.util.Scanner;

public class ShopCarTest {

    public static void main(String[] args) {
        Goods[] shopCar=new Goods[100];
        //搭建操作架构
        while (true) {
            System.out.println("请选择如下命令进行操作:");
            System.out.println("添加商品到购物车:add");
            System.out.println("查询商品到购物车:query");
            System.out.println("修改商品到购物车:update");
            System.out.println("结算商品购买金额:pay");
            Scanner sc=new Scanner(System.in);
            System.out.println("请你输入命令:");
            String command=sc.next();
            switch (command){
                case "add":
                    addGoods(shopCar,sc);
                    break;
                case"query":
                    queryGoods(shopCar);
                    break;
                case"update":
                    updateGoods(shopCar,sc);
                    break;
                case"pay":
                    payGoods(shopCar);
                    break;
                default:
                    System.out.println("没有该功能");;
            }
        }
    }
    /*
    * 添加商品
    * */
    private static void addGoods(Goods[] shopCar,Scanner sc) {
    //添加商品界面

            System.out.println("请输入商品编号:");
            int id=sc.nextInt();
            System.out.println("请输入商品名称:");
            String name=sc.next();
            System.out.println("请输入商品价格:");
            double price=sc.nextDouble();
            System.out.println("请输入商品数量:");
            int buyNumber=sc.nextInt();

            //将新添加的商品属性封装金goods对象中
            Goods g=new Goods();
            g.id=id;
            g.name=name;
            g.price=price;
            g.buyNumber=buyNumber;
            //封装好之后 放进数组
            for (int i = 0; i < shopCar.length; i++) {
                if(shopCar[i]==null){
                    shopCar[i]=g;//商品存入了
                    break;
                }
            }
            System.out.println(g.name+"添加成功");



    }
    /*
    * 查询商品
    * */
    private static void queryGoods(Goods[] shopCar) {
        //查询商品信息
        System.out.println("===============查询商品===================");
        System.out.println("编号\t\t名称\t\t价格\t\t商品数量\t\t");
        //遍历数组 然后碰到数组中de null则停止遍历
        for (int i = 0; i < shopCar.length; i++) {
            //给个对象接受数组中的元素 数组中的地址指向对象
            Goods g=shopCar[i];
            //做个判断 如果不是Null则展示
            if(shopCar[i]!=null){
                System.out.println(g.id+"\t\t"+g.name+"\t\t"+g.price+"\t\t"+g.buyNumber);
            }else{
                //否则就遍历结束
                break;
            }

        }


    }
    /*
    * 修改商品
    * */
    private static void updateGoods(Goods[] shopCar,Scanner sc) {
        //根据id找到商品对象 然后进行修改
        System.out.println("请你输入商品id ");
        int id=sc.nextInt();
        //拿到商品对象
        Goods g=getid(shopCar,id);
        while (true) {
            if(g==null){
                System.out.println("找不到该商品");
            }
            else{
                System.out.println("请你输入要修改"+g.name+"的数量：");
                int buyNumber=sc.nextInt();
                g.buyNumber=buyNumber;
                System.out.println("修改完成");
                queryGoods(shopCar);
                break;
            }
        }

    }
    //写一个方法根据id拿到goods对象 返回goods对象
    public static Goods getid(Goods[] shopCar,int id){
        //根据id返回
        for (int i = 0; i < shopCar.length; i++) {
            Goods g=shopCar[i];
            if(shopCar[i]!=null){
                if(g.id==id){
                    return g;
                }
            }
            else{
                return null;
            }
        }
        //遍历完之后发现都是Null;
            return null;

    }
    /*
    * 结算商品
    * */
    private static void payGoods(Goods[] shopCar) {
    //查询之前先展示购物车里面的商品
        queryGoods(shopCar);
        double money=0;
        //遍历数组
        for (int i = 0; i < shopCar.length; i++) {
            Goods g=shopCar[i];
            if(g!=null){
                money+=(g.buyNumber*g.price);
            }
            else{
                break;
            }

        }
        System.out.println("商品总金额为："+money);



    }

}
